package com.wan.activity

import android.content.Intent
import android.content.IntentFilter
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.content.LocalBroadcastManager
import android.support.v7.widget.SearchView
import android.util.Log
import com.wan.R
import com.wan.adapter.SearchNvpAdapter
import com.wan.fragment.SearchResultFragment
import com.wan.fragment.SearchShowFragment
import com.wan.receiver.SearchChangeTextReceiver
import com.wan.uitls.CommonUtils
import com.wan.uitls.QueryTextChangeListener
import com.wan.uitls.ResultRefreshDataListener
import kotlinx.android.synthetic.main.activity_search.*

/**
 * 搜索界面
 */
class SearchActivity : AppCompatActivity(), SearchView.OnQueryTextListener,  QueryTextChangeListener {



    private val mThreadName = this.javaClass.name


    private lateinit var textChange: SearchChangeTextReceiver


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)
        initView()
        initData()
    }

    private fun initData() {
        textChange = SearchChangeTextReceiver(this)
        var intentFilter = IntentFilter(CommonUtils.Action_Search_Change_Text)
        LocalBroadcastManager.getInstance(this).registerReceiver(textChange, intentFilter)
    }

    private fun initView() {
        search_search_sv.setOnQueryTextListener(this)
        search_search_sv.isIconified = false
        search_search_sv.clearFocus()
        val fragmentList: ArrayList<Fragment> = ArrayList()
        fragmentList.add(SearchShowFragment())
        fragmentList.add(SearchResultFragment())
        search_content_nvp.adapter = SearchNvpAdapter(supportFragmentManager, fragmentList)
        search_content_nvp.currentItem = 0
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        Log.d(mThreadName, " onQueryTextChange: 提交中的文字==> $newText")
        if (newText.isNullOrBlank()) {
            search_content_nvp.currentItem = 0
        }
//        else {
//            search_content_nvp.currentItem = 1
//        }
        return true
    }

    override fun onQueryTextSubmit(query: String?): Boolean {
        Log.d(mThreadName, " onQueryTextSubmit: 提交中的文字==> $query")
        if (query.isNullOrEmpty()) {
            Log.d(mThreadName, " onQueryTextSubmit: 提交中的文字==>query is null")
            search_content_nvp.currentItem = 0
            return true
        }
        search_content_nvp.currentItem = 1
        var intent = Intent(CommonUtils.Action_Result_Fragment_Refresh)
        intent.putExtra(CommonUtils.Search_Text, query)
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
        return true

    }

    override fun onQueryTextChangeListener(intent: Intent) {
        var changeText = intent.getStringExtra(CommonUtils.Search_Change_Text)
        search_content_nvp.currentItem = 1
        search_search_sv.setQuery(changeText, false)
    }


    override fun onDestroy() {
        super.onDestroy()
        LocalBroadcastManager.getInstance(this).unregisterReceiver(textChange)
    }

}
